// pages/index/medicineReminder/editMedicine/editMedicine.js
import request from '../../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    name: '',
    isEnabled: false,
    timeList: '',
    dateObj:'',
    reminderDays: [
      { id: 1, name: '每隔1天', value: 1 },
      { id: 2, name: '每隔2天', value: 2 },
      { id: 3, name: '每隔3天', value: 3 }
    ],
    selectedReminderDay: {},
    selectedUnit: '片',
    dosage: '2',
    showTimePicker: false,
    hour: '00',
    minute: '00',
    hourIndex: 0,
    minuteIndex: 0,
    hours: Array.from({length: 24}, (_, i) => i < 10 ? '0' + i : '' + i),
    minutes: Array.from({length: 60}, (_, i) => i < 10 ? '0' + i : '' + i)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 可以从options获取编辑的药品信息
    let iid=options.id;
    this.setData({
      id:iid
    })
    console.log(options)
    this.selectById(iid)
  },
  selectById(iid){
    let that=this
    request({
      url: 'tixing/tixing/'+iid,
      method:'GET',
      success:(res)=>{
        console.log(res.data.data)
        let result=res.data.data
        that.setData({
          timeList:result.time,
          selectedReminderDay:{
            id:result.days,
            name:'每隔'+result.days+'天',
            value:result.days
          },
          name:result.name,
          selectedUnit:result.dan,
          dosage:result.num
        })
      }
    })
  },
  add(){
    request({
      url: 'tixing/tixing',
      data: {
        id:this.data.id,
        days:this.data.selectedReminderDay.value,
        dan:this.data.selectedUnit,
        num:this.data.dosage,
        time:this.data.timeList
      },
      method:"PUT",
      success: (result) => {
        console.log(111)
      },
      fail: (err) => {},
      complete: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onNameInput(e) {
    this.setData({
      name: e.detail.value
    });
  },

  onDosageInput(e) {
    this.setData({
      dosage: e.detail.value
    });
  },

  onTimeTap() {
    this.setData({
      showTimePicker: true
    });
  },

  cancelTime() {
    this.setData({
      showTimePicker: false
    });
  },

  confirmTime() {
    const { hour, minute } = this.data;
    const newTime = `${hour}:${minute}`
    console.log(newTime)
    const newTimeList =  newTime;
      this.setData({
        timeList: newTimeList,
        dateObj:baseDate,
        showTimePicker: false
      });
  },

  bindTimeChange(e) {
    const values = e.detail.value;
    const hour = this.data.hours[values[0]];
    const minute = this.data.minutes[values[1]];
    
    this.setData({
      hour,
      minute,
      hourIndex: values[0],
      minuteIndex: values[1]
    });
  },

  deleteTime(e) {
    const { index } = e.currentTarget.dataset;
    const timeList = [...this.data.timeList];
    timeList.splice(index, 1);
    this.setData({
      timeList
    });
  },

  onReminderDaysTap() {
    wx.showActionSheet({
      itemList: this.data.reminderDays.map(item => item.name),
      success: (res) => {
        this.setData({
          selectedReminderDay: this.data.reminderDays[res.tapIndex]
        });
      }
    });
  },

  onUnitTap() {
    wx.showActionSheet({
      itemList: ['片', '粒', '毫升', '克'],
      success: (res) => {
        const units = ['片', '粒', '毫升', '克'];
        this.setData({
          selectedUnit: units[res.tapIndex]
        });
      }
    });
  },

  onSwitchChange(e) {
    this.setData({
      isEnabled: e.detail.value
    });
  },
  delete(){
    request({
      url: 'tixing/tixing/'+this.data.id,
      method: 'DELETE',
      success: (result) => {},
      fail: (err) => {},
      complete: (res) => {},
    })
  },
  onDelete() {
    wx.showModal({
      title: '提示',
      content: '确定要删除这条用药提醒吗？',
      success: (res) => {
        if (res.confirm) {
          // 执行删除操作
          this.delete();
          wx.navigateBack();
        }
      }
    });
  },

  onSave() {
    // 处理保存逻辑
    wx.showToast({
      title: '保存成功',
      icon: 'success',
      success: () => {
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      }
    });
  }
})